/*
 * @Author: your name
 * @Date: 2024-05-24 17:59:00
 * @LastEditTime: 2024-06-06 17:35:39
 * @LastEditors: LYNN
 * @Description: In User Settings Edit
 * @FilePath: \memorandum\vue3-memo\src\utils\index.ts
 */
import datjs from "dayjs";
export const FormatTime = (date: Date | string) => {
  return datjs(date).format("YYYY/MM/DD hh:mm");
};
// 获取 localStorage
export const localGetItem = (key: string): any => {
  let data_str = localStorage.getItem(key);
  if (data_str) {
    return JSON.parse(data_str);
  }
  return null;
};
// 设置 localStorage
export const localSetItem = (key: string, value: any): void => {
  localStorage.setItem(key, JSON.stringify(value));
};

// 生成随机 ID
export const geneId = (): number => {
  return Math.floor(Math.random() * 939874);
};

// 模拟http请求
export const ImitateHttp = (
  fun: (s: Function, f: Function) => void,
  timer = 1000
) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => fun(resolve, reject), timer);
  });
};

// 防抖函数
export const debounce = (fn: Function, delay = 1600) => {
  let timer: ReturnType<typeof setTimeout> | null = null; // 使用 setTimeout 的返回类型

  return (...args: any[]) => {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn(...args);
    }, delay);
  };
};
